/*******************************************************************************
 *  * Copyright 2013 Impetus Infotech.
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 ******************************************************************************/
package com.impetus.client.crud.autogeneratedid;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.impetus.client.utils.MongoUtils;

public class EmployeeCubicleTest
{
    private static final String _PU = "mongoTest";

    /** The emf. */
    private static EntityManagerFactory emf;

    /** The em. */
    private static EntityManager em;

    @Before
    public void setUp() throws Exception
    {
        emf = Persistence.createEntityManagerFactory(_PU);
        em = emf.createEntityManager();
    }

    @Test
    public void testUser()
    {
        EmployeeCubicle emp1 = new EmployeeCubicle();
        emp1.setName("test1");

        Cubicle cube1 = new Cubicle();
        cube1.setName("Cube 1");

        cube1.setResidentEmployee(emp1);
        emp1.setAssignedCubicle(cube1);

        em.persist(emp1);
        em.clear();
        
        EmployeeCubicle emp2 = new EmployeeCubicle();
        emp2.setName("test2");

        Cubicle cube2 = new Cubicle();
        cube2.setName("Cube 2");

        Query q = em.createQuery("select e from EmployeeCubicle e where e.name = 'test1'");
        List<EmployeeCubicle> employees = q.getResultList();

        Assert.assertNotNull(employees);
        EmployeeCubicle employee = employees.get(0);
        Assert.assertNotNull(employee.getAssignedCubicle());
        Assert.assertEquals("Cube 1", employee.getAssignedCubicle().getName());
        Assert.assertEquals(employee.getEMPID(), cube1.getResidentEmployee().getEMPID());

        employee.setAssignedCubicle(cube2);

        em.merge(employee);
        em.clear();
        
        q = em.createQuery("select e from EmployeeCubicle e where e.name = 'test1'");
        employees = q.getResultList();
        Assert.assertNotNull(employees);
        employee = employees.get(0);
        Assert.assertNotNull(employee.getAssignedCubicle());
        Assert.assertEquals("Cube 2", employee.getAssignedCubicle().getName());
        em.clear();

    }

    @After
    public void tearDown()
    {
        em.close();
        MongoUtils.dropDatabase(emf, _PU);
        emf.close();
        
    }

}
